package com.semp.gu.ui.elements;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.semp.gu.domain.local.LocalAlbumElement;
import com.semp.gu.domain.local.LocalAlbumElement.AlbumElementType;
import com.semp.gu.ui.UIConstants;

public class ElementPanel extends JPanel {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -972643076020535789L;
	private final static Logger	logger				= LoggerFactory.getLogger(ElementPanel.class);

	private LocalAlbumElement	localAlbumElement;
	private boolean				selected;

	public ElementPanel(LocalAlbumElement localAlbumElement) {
		super();
		this.localAlbumElement = localAlbumElement;

		/*
		 * Content
		 */
		this.setBorder(BorderFactory.createEtchedBorder());

		/*
		 * Sizing
		 */
		this.setMaximumSize(new Dimension(UIConstants.PANEL_ELEMENT_WIDTH, UIConstants.PANEL_ELEMENT_HEIGHT));
		this.setPreferredSize(this.getMaximumSize());
		this.setSize(this.getMaximumSize());
		this.setVisible(false);

		logger.debug("Added Element {}", localAlbumElement.getName());
		
		
		/*
		 * Invoke later : picture display
		 */
		//SwingUtilities.invokeLater(new Runnable() {
			//public void run() {
				displayElement();
			//}
		//});
	}

	private void displayElement() {
		if (localAlbumElement.getType() == AlbumElementType.PICTURE || localAlbumElement.getType() == AlbumElementType.MOVIE) {
			Image tmpImage;
			if (localAlbumElement.getType() == AlbumElementType.PICTURE) {
				tmpImage = Toolkit.getDefaultToolkit().getImage(localAlbumElement.getFile().getAbsolutePath());
			} else {
				tmpImage = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("com/semp/gu/ui/img/movie.icon.jpg"));
			}
			MediaTracker mTracker = new MediaTracker(this);
			mTracker.addImage(tmpImage, 1);
			try {
				mTracker.waitForID(1);
			} catch (InterruptedException e) {
				logger.error("", e);
			}
			/*
			 * Thumb size calculation
			 */
			int imgWidth, imgHeight, thumbWidth, thumbHeight;
			imgWidth = tmpImage.getWidth(null);
			imgHeight = tmpImage.getHeight(null);
			logger.debug("Image size {}, {}", imgWidth, imgHeight);
			if (imgWidth > imgHeight) {
				thumbWidth = UIConstants.PANEL_ELEMENT_THUMB_MAXWIDTH;
				thumbHeight = thumbWidth * imgHeight / imgWidth;
			} else {
				thumbHeight = UIConstants.PANEL_ELEMENT_THUMB_MAXHEIGHT;
				thumbWidth = thumbHeight * imgWidth / imgHeight;
			}

			/*
			 * Thumb display
			 */
			Image thumbImage = tmpImage.getScaledInstance(thumbWidth, thumbHeight, 0);
			ImageIcon icon = new ImageIcon(thumbImage);
			JLabel label = new JLabel();
			label.setIcon(icon);
			this.add(label);
		}
		
		this.add(new JLabel(localAlbumElement.getName()));
		this.setVisible(true);
		logger.debug("Displayed Element {}", localAlbumElement.getName());
	}
	
	
	// -------------------- Getters & setters
	public LocalAlbumElement getLocalAlbumElement() {
		return localAlbumElement;
	}

	public void setLocalAlbumElement(LocalAlbumElement localAlbumElement) {
		this.localAlbumElement = localAlbumElement;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

}
